package com.itheima.test;

import net.sf.jasperreports.engine.*;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * 基于parameters以Map的形式填充数据
 *
 * @author mewp
 * @date 2022/12/12 10:45
 **/
public class PDFDemo1 {
    public static void main(String[] args) throws Exception {
        String filePath = "D:\\mewp\\test\\test01.jasper";
        //文件的输入流
        FileInputStream is = new FileInputStream(filePath);
        //创建JasperPrint，向jasper文件中填充数据
        FileOutputStream os = new FileOutputStream("D:\\mewp\\test\\demo01.pdf");
        try {
            Map parameters = new HashMap<>();
            //设置参数 参数的key = 模板中使用的parameters参数的name
            parameters.put("name", "张三");
            parameters.put("phone", "13800000000");
            //将JasperPrint以PDF的形式输出
            JasperPrint jasperPrint = JasperFillManager.fillReport(is, parameters, new JREmptyDataSource());
            //导出
            JasperExportManager.exportReportToPdfStream(jasperPrint, os);
        } catch (JRException e) {
            e.printStackTrace();
        } finally {
            os.flush();
        }

    }
}
